<?php
class ShowAction extends Action{
	public function index(){
		$id=intval($_GET['id']);
		$db=D('GoodsView');
		$data=$db->getAll($id);
		//商品的分类id
		$list=M('goods');
		$cid=$list->where(array('id'=>$id))->getField('cid');
		//返回父类id及名称
		$dbs=M('category');
		$arr=$dbs->select();
		$parent=parentid($arr,$cid);
		//返回同级分类
		$fid=$dbs->where(array('id'=>$cid))->getField('pid');
		$brother=$dbs->where(array('pid'=>$fid))->field(array('id','name'))->select();
		//p($parent);
		//echo $cid;

		/*p($_SESSION);
		die;*/
		//分配同级分类
		$this->brother=$brother;
		//分配所有父级分类
		$this->parent=$parent;
		$this->data=$data;
		$this->display();
	}
	//选择规格与价格
	public function choose_spec(){
		if(!$this->isAjax()){
			halt('页面不存在');
		}
		$gaid=$_POST['gaid'];
		$str_gaid=implode('|',$gaid);
		$gid=intval($_POST['gid']);
		$goods_list=M('goods_list');//在货品表中查找是否有该货品
		$result=$goods_list->where(array('attr'=>$str_gaid))->field(array('inventory'))->find();
		/*echo($str_gaid);die;*/
		if((!$result) or $result['inventory']==0){
			echo json_encode(array('status'=>0,'inventory'=>0));
			die;
		}
		$goods=M('goods');
		$sprice=$goods->where(array('id'=>$gid))->getField('sprice');
		$db=M('goods_attr');
		$price=$db->where(array('id'=>array('IN',$gaid)))->sum('price');
		echo json_encode(array(
			'status'=>1,
			'inventory'=>$result['inventory'],
			'price'=>number_format($sprice+$price,2)
			));
	}
	//异步加入购物车
	public function addCar(){
		if(!$this->isAjax()){
			halt('页面不存在');
		}
		$aids=$_POST['aid'];
		$gid=intval($_POST['gid']);
		$num=intval($_POST['num']);
		$aid=implode('|',$aids);
		$db=M('goods_list');
		$id=$db->where(array('gid'=>$gid,'attr'=>$aid))->getField('id');
		if(!$id){
			echo json_encode(array('status'=>0));
			die;
		}

		$goods=M('goods');
		$sprice=$goods->where(array('id'=>$gid))->field(array('name','sprice'))->find();
		$goods_attr=M('goods_attr');
		$price=$goods_attr->where(array('gid'=>$gid,'id'=>array('IN',$aids)))->sum('price');
		$price=$sprice['sprice']+$price;
		$guiges=array();
		foreach($aids as $v){
			$guiges[]=$goods_attr->where(array('gid'=>$gid,'id'=>$v))->getField('value');
		}
		$guige=implode('/',$guiges);


		$_SESSION['car'][$id]=array(
			'gid'=>$gid,
			'num'=>$num,
			'spec'=>$aid,
			'price'=>$price,
			'guige'=>$guige
			);
		$count=0;
		$allprice=0;
		foreach($_SESSION['car'] as $v){
			$count+=$v['num'];
			$allprice+=$v['num']*$v['price'];
		};
		$_SESSION['allprice']=$allprice;
		$_SESSION['car_count']=$count;

		
		echo json_encode(array('status'=>1,'count'=>$count));
	}
	/**
	 * 查看购物车信息
	 */
	public function min_car(){
		$allprice=$_SESSION['allprice'];
		$car_count=$_SESSION['car_count'];
		$db=M('goods');
		$str="";
		foreach($_SESSION['car'] as $k=>$v){
			$str.="<dl><dt>";
			$result=$db->where(array('id'=>$v['gid']))->field(array('name','listpic'))->find();
			$str.="<img src='".__ROOT__."/Uploads/List/".$result['listpic']."'/></dt><dd class='goods_name'><a href='".U('Show/index',array('id'=>$v['gid']))."'>";
			$str.=$result['name']."(".$v['guige'].")</a></dd>";
			$str.="<dd><p><span>¥".number_format($v['price'],2)."</span> <b>X".$v['num']."</b></p>";
			$str.="<p><a class='car_del' lid=".$k." href='".U('Show/cardel')."'>删除</a></p></dd></dl>";
		}
		$str.="<div class='car_foot'><p>购物车中共<span class='goods_number'>".$car_count."</span> 件商品</p>";
		$str.="<p>共计：<span class='countprice'>¥".number_format($allprice,2)."</span></p>";
		$str.="<a href='".U('Car/index')."'>结算</a></div>";
		if(isset($_SESSION['car'])){
			echo json_encode(array( 'status'=>1,'str'=>$str));	
		}else{
			echo json_encode(array('status'=>0));
		}
		
	}
	//删除min-car中的信息
	public function cardel(){
		if(!$this->isAjax()){
			halt('页面不存在');
		}
		$lid=intval($_POST['lid']);
		$num=$_SESSION['car'][$lid]['num'];
		$car_count=$_SESSION['car_count'];
		$_SESSION['car_count']=$car_count-$num;
		$price=$_SESSION['car'][$lid]['price'];
		$_SESSION['allprice']=$_SESSION['allprice']-($price*$num);
		unset($_SESSION['car'][$lid]);
		if($_SESSION['car_count']==0){
			unset($_SESSION['car_count']);
			unset($_SESSION['allprice']);
			unset($_SESSION['car']);
			echo json_encode(array('status'=>0));
		}else{
			echo json_encode(array('status'=>1,'allprice'=>number_format($_SESSION['allprice'],2),'car_count'=>$_SESSION['car_count']));
		}

	}
	
}
?>